package com.edfu.core.institutemgt.persistence.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.edfu.core.institutemgt.persistence.api.IInstituteMgtPersistence;
import com.edfu.core.institutemgt.persistence.entity.AcadSemEntity;
import com.edfu.core.institutemgt.persistence.entity.BatchEntity;
import com.edfu.core.institutemgt.persistence.entity.BranchEntity;
import com.edfu.core.institutemgt.persistence.entity.BranchMemberEntity;
import com.edfu.core.institutemgt.persistence.entity.IInstituteMgtEntity;
import com.edfu.core.institutemgt.persistence.entity.InstituteEntity;
import com.edfu.core.institutemgt.persistence.entity.SectionEntity;

public class InstituteMgtPersistenceImpl implements IInstituteMgtPersistence {
	private EntityManager entityManager;
	@PersistenceContext
	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}
	
	@Override
	public BranchEntity getBranch(String branchCode) {
		Query q = entityManager.createQuery("select b from BranchEntity b where code = :branchCode ");
		q.setParameter("branchCode", branchCode);
		BranchEntity be = (BranchEntity) q.getSingleResult();
		return be;
	}
	
	@Override
	public AcadSemEntity getSemester(String semesterCode) {
		Query q = entityManager.createQuery("select a from AcadSemEntity a where code = :semesterCode ");
		q.setParameter("semesterCode", semesterCode);
		AcadSemEntity ase = (AcadSemEntity) q.getSingleResult();
		return ase;
	}
	
	@Override
	public BatchEntity getBatch(String batchCode) {
		Query q = entityManager.createQuery("select b from BatchEntity b where code = :batchCode ");
		q.setParameter("batchCode", batchCode);
		BatchEntity be = (BatchEntity) q.getSingleResult();
		return be;
	}
	
	@Override
	public BranchMemberEntity getBranchMember(String userId, String userRole, String branchCode) {
		Query q = entityManager.createQuery("select e from BranchMemberEntity e where e.member.userId = :userId and e.role.name = :userRole and e.branch.code = :branchCode");
		q.setParameter("userId", userId);
		q.setParameter("userRole", userRole);
		q.setParameter("branchCode", branchCode);
		List<?> resultList = q.getResultList();
		if (resultList == null || resultList.size() == 0)
			return null ;
		else 
			return (BranchMemberEntity)resultList.get(0);
	}
	
	@Override
	public InstituteEntity getInstitute(String instituteCode) {
		Query q = entityManager.createQuery("select i from InstituteEntity i where code = :instituteCode ");
		q.setParameter("instituteCode", instituteCode);
		InstituteEntity ie = (InstituteEntity) q.getSingleResult();
		return ie;
	}

	@Override
	public void persist(IInstituteMgtEntity entity) {
		entityManager.persist(entity);
	}

	@Override
	public SectionEntity getSection(String sectionCode) {
		Query q = entityManager.createQuery("select e from SectionEntity e where e.code = :sectionCode ");
		q.setParameter("sectionCode", sectionCode);
		SectionEntity se = (SectionEntity) q.getSingleResult();
		return se;
	}

}
